<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增菜品')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">

<!--        <input name="categoryList" id="categoryList" type="hidden" th:value="${categoryList}">-->

        <form class="form-horizontal m" id="form-menu-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">菜品名称：</label>
                <div class="col-sm-8">
                    <input name="menuName" class="form-control" type="text">
                </div>
            </div>

<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label is-required">菜品类型：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <select name="categoryId" id="categoryId" lay-verify="required" class="form-control">-->
<!--                        <option th:each="category:${categoryList}" th:value="${category.id}"-->
<!--                                th:text="${category.name}"></option>-->
<!--                    </select>-->
<!--                </div>-->
<!--            </div>-->

            <div class="form-group">
                <label class="col-sm-3 control-label">菜品图片：</label>
                <div id="imgDiv" class="col-sm-8">
                    <input  type="file" name="file" id="file0" multiple="multiple" />
                </div>
            </div>

            <div class="form-group">    
                <label class="col-sm-3 control-label">菜品描述：</label>
                <div class="col-sm-8">
                    <input name="menuDescription" class="form-control" type="text">
                </div>
            </div>

        </form>
    </div>
    <th:block th:include="include :: footer" />

    <script type="text/javascript">
        var prefix = ctx + "wechatprogram/menu";

        $("#file0").change(function(){
            // var objUrl = getObjectURL(this.files[0]) ;
            var objUrl = getObjectURL(this.files[0]) ;
            if (objUrl) {
                $("#imgDiv").html();
                $('#imgDiv').append('<img src="' + objUrl + '" id="img0" style="width: 200px;height: 150px;" >');
                // $("#img0").attr("src", objUrl) ;
            }
        }) ;
        //建立一個可存取到該file的url
        function getObjectURL(file) {
            var url = null ;
            if (window.createObjectURL!=undefined) { // basic
                url = window.createObjectURL(file) ;
            } else if (window.URL!=undefined) { // mozilla(firefox)
                url = window.URL.createObjectURL(file) ;
            } else if (window.webkitURL!=undefined) { // webkit or chrome
                url = window.webkitURL.createObjectURL(file) ;
            }
            return url ;
        }

        function submitHandler() {

            var menuName = $("input[name=menuName]").val();
            var file = document.getElementById('file0').files[0];
            var menuDescription = $("input[name=menuDescription]").val();
            var formdata = new FormData();
            formdata.append("menuName", menuName);
            formdata.append("file", file);
            formdata.append("menuDescription", menuDescription);
            $.ajax({
                url:  prefix + "/add",
                data: formdata,
                type: "post",
                processData: false,
                contentType: false,
                success: function(result) {
                    $.operate.saveSuccess(result);
                }
            })
        }
    </script>
</body>
</html>